<?php

class MY_Form_validation extends CI_Form_validation
{

	private $error;

	public function __construct()
	{
		parent::__construct();
	}

	//自定义表单权限验证
	//验证用户权限
	public function checkRightsForm($right_ids)
	{
		$this->CI->load->model('Right_model');
		if ($this->CI->Right_model->checkRights($right_ids)) {
			return true;
		}
		$this->CI->form_validation->set_message('checkRightsForm', '无效的 %s 设置');
		return false;
	}

	public function unique($value, $model)
	{
		$a = explode(',', $model);
		$model = $a[0];
		$this->CI->load->model($model);
		if (count($a) == 1)
		{
			if ($this->CI->$model->unique($value))
			{
				return true;
			}
		}
		else
		{
			if (isset($a[2]))
			{
				if ($this->CI->$model->unique($value, $a[1], true))
				{
					return true;
				}
			}
			if ($this->CI->$model->unique($value, $a[1]))
			{
				return true;
			}
		}
		$this->CI->form_validation->set_message('unique', "{$value} 已存在，请重新输入");
		return false;
	}

	//验证当前用户
	public function checkSelf($val)
	{
		$this->CI->load->model('User_model');
		if ($this->CI->User_model->_checkUser($this->CI->input->cookie('username'), $val))
		{
			return true;
		}
		$this->CI->form_validation->set_message('checkSelf', "%s 不正确，请重试");
		return false;
	}

	//验证选择的type是否存在
	public function existType($val, $model)
	{
		$this->CI->load->model($model);
		if ($this->CI->$model->getOne($val))
		{
			return true;
		}
		$this->CI->form_validation->set_message('existType', "不存在");
		return false;
	}

	//验证时候是有效的网址
	public function is_url($val)
	{
		$pattern = '/^(((ht|f)tp(s?))\:\/\/)?(www.|[a-zA-Z].)[a-zA-Z0-9\-\.]+\.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk)(\:[0-9]+)*(\/($|[a-zA-Z0-9\.\,\;\?\'\\\+&amp;%\$#\=~_\-]+))*$/';
		if ( preg_match($pattern, $val, $matches) )
		{
			if ('http://' != $matches[1])
			{
				return 'http://' . $val;
			}
			return $val;
		}
		return false;
	}

	//Supplier_model:contact
	public function is_contact($val)
	{
		$this->CI->load->model('Supplier_model');
		$this->CI->form_validation->set_message('is_contact', "格式不正确");
		return $this->CI->Supplier_model->contact_validation($val);
	}

	//Sub_goods_model:form_validation;
	public function is_supplier($val)
	{
		$this->CI->load->model('Sub_goods_model');
		$this->CI->form_validation->set_message('is_supplier', "无效的供应商");
		return $this->CI->Sub_goods_model->form_validation('is_supplier', $val);
	}

	//验证标识符
	public function check_flg($flg, $type)
	{
		if ('inbound' == $type)
		{
			switch ($flg) {
				case 'order_flg':
				case 'return_flg':
				case 'exchange_flg':
				case 'other_flg':
					return $flg;
				default:
					$this->CI->form_validation->set_message('check_flg', "无效标识符");
					break;
			}
		}
		else if ('outbound' == $type)
		{
			switch ($flg) {
				case 'sale_flg':
				case 'return_flg':
				case 'exchange_flg':
				case 'other_flg':
					return $flg;
				default:
					$this->CI->form_validation->set_message('check_flg', "无效标识符");
					# code...
					break;
			}
		}
		$this->CI->form_validation->set_message('check_flg', "无效标识符");
		return FALSE;
	}

}